package com.yudean.esb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * 接口测试文档
 */
@EnableSwagger2         // 开启Swagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment){
        //配置要显示Swagger的环境
        Profiles profiles = Profiles.of("dev","test");
        //获取项目的环境
        final boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("ERPWebService")
                .enable(flag)       // 控制是否开启Swagger
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yudean.esb.controller"))
                .build();
    }

    /**
     *  构建 api文档的信息
     */
    private ApiInfo apiInfo(){
        Contact contact = new Contact("erp", "", "erp@dms.yudean.com");
        return new ApiInfo("ERP WebService Api Documentation",
                "ERP接口测试文档",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "https://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
